from typing import Union
from typing import List
from fastapi import FastAPI
from fastapi import Query, Body
from pydantic import BaseModel 
from starlette.staticfiles import StaticFiles


app = FastAPI()

# 添加静态目录
app.mount('/static', StaticFiles(directory='static'), name='static')

# param => model
class Item(BaseModel):
    name: str
    price: float
    is_offer: Union[bool, None]

@app.get("/")
def hello():
    return {"msg": "hello"}

# 路径参数 + query参数
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
    return {"item_id": item_id, "q": q}

@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
    return {"item_id": item_id, "item_name": item.name, 
            "item_price": item.price, "item_offer": item.is_offer}

@app.get("/process_items")
async def process_items(items: List[str] = Query(None)):
    for i in items:
        print(i)
    return {"msg": items}
